package com.etc.service.impl;

import com.etc.controller.DoctorController;
import com.etc.dao.DoctorMapper;
import com.etc.dao.HistoryMapper;
import com.etc.dao.PatientMapper;
import com.etc.dao.TimeMapper;
import com.etc.entity.Doctor;
import com.etc.entity.History;
import com.etc.entity.Patient;
import com.etc.entity.Time;
import com.etc.service.DoctorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private HistoryMapper historyMapper;

    @Autowired
    private TimeMapper timeMapper;

    @Override
    public List<Time> getTimeByDoctorId(String doctorId) {
        Time time=new Time();
        time.setDoctorId(doctorId);
        List<Time> times = timeMapper.findByTime(time);
        return times;
    }

    @Override
    public boolean addDoctor(String doctorName, Integer deptId, String phone, String gender, String image) {
        Doctor doctor = new Doctor();
        doctor.setDoctorId(UUID.randomUUID().toString().replace("-", ""));
        doctor.setDoctorName(doctorName);
        doctor.setDeptId(deptId);
        doctor.setPhone(phone);
        //默认密码
        doctor.setPassword("123456");
        doctor.setGender(gender);
        doctor.setImage(image);

        int i = doctorMapper.insertSelective(doctor);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean existOfPhone(String phone) {
        Doctor doctor = new Doctor();
        doctor.setPhone(phone);
        List<Doctor> list = doctorMapper.findSelective(doctor);
        return !list.isEmpty();
    }

    @Override
    public Doctor login(String phone, String password) {
        Doctor doctor = new Doctor();
        doctor.setPhone(phone);
        doctor.setPassword(password);
        List<Doctor> list = doctorMapper.findAllSelective(doctor);
        if (!list.isEmpty()) {
            Doctor loginDoctor = list.get(0);
            request.getSession().setAttribute("doctor", loginDoctor);
            return loginDoctor;
        }
        return null;
    }

    @Override
    public void logout() {
        request.getSession().removeAttribute("doctor");

    }

    @Override
    public Doctor getDoctorBySession() {
        return (Doctor) request.getSession().getAttribute("doctor");
    }

    @Override
    public boolean updateDoctor(String doctorId, String doctorName, String password, Integer deptId, String phone, String gender, String image) {
        Doctor doctor = new Doctor();
        doctor.setDoctorId(doctorId);
        doctor.setDoctorName(doctorName);
        doctor.setPassword(password);
        doctor.setDeptId(deptId);
        doctor.setPhone(phone);
        doctor.setGender(gender);
        doctor.setImage(image);

        int i = doctorMapper.updateSelectiveById(doctor);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Patient> getPatientDetail(String patientId) {
        Patient patient=new Patient();
        patient.setPatientId(patientId);
        return patientMapper.findSelective(patient);
    }

    @Override
    public List<History> getHistoryDetail(String patientId) {
        History history=new History();
        history.setPatientId(patientId);
        return historyMapper.findHistoryAndDoctorAndPatientByPatientId(patientId,null);
    }

    @Override
    public List<History> getPatientAndHistoryByDoctorId(String doctorId) {
        History history=new History();
        history.setDoctorId(doctorId);
        return historyMapper.findHistoryAndDoctorAndPatientByPatientId(null,doctorId);
    }

    @Override
    public List<Doctor> getDoctorList() {
        List<Doctor> doctorList = doctorMapper.findAllSelective(null);
        return doctorList;
    }

    @Override
    public List<Doctor> getDoctorListSelective(Doctor doctor) {
        List<Doctor> doctorList = doctorMapper.findAllSelective(doctor);
        return doctorList;
    }

    @Override
    public PageInfo<Doctor> getDoctorListByPage(Doctor doctor, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Doctor> doctorList = doctorMapper.findAllSelective(doctor);
        PageInfo<Doctor> pageInfo = new PageInfo<>(doctorList);
        return pageInfo;
    }

}
